/*
 * Created by JFormDesigner on Sat Jun 19 17:00:38 GMT+08:00 2021
 */

package com.bk.view;

import com.bk.dao.UserDao;
import com.bk.model.User;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
public class uEditFrame extends JFrame {
    User user;

    public uEditFrame(User user) {
        initComponents();
        this.user = user;
        textField1.setText(user.getUname());
        textField2.setText(user.getUpassword());
    }

    // 确定修改，监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        boolean bool = false;
        String credit = comboBox1.getSelectedItem().toString();
        try {
            bool = new UserDao().editUser(user, textField1.getText(), textField2.getText(), credit);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (bool) {
            JOptionPane.showMessageDialog(null, "修改成功！");
            dispose();
        }else{
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    // 重置，监听
    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        textField1.setText("");
        textField2.setText("");
    }

    private void textField1KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = textField1.getText();
        if (s.length() > 7) {
            e.consume();
        }
    }

    private void textField2KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = textField2.getText();
        if (s.length() > 11) {
            e.consume();
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        textField1 = new JTextField();
        label1 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        label4 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();
        comboBox1 = new JComboBox<>();

        //======== this ========
        setTitle("\u7528\u6237\u7f16\u8f91");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- textField1 ----
        textField1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                textField1KeyTyped(e);
            }
        });

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- textField2 ----
        textField2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                textField2KeyTyped(e);
            }
        });

        //---- label3 ----
        label3.setText("\u4fe1\u7528");
        label3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- label4 ----
        label4.setText("\u5bc6\u7801");
        label4.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u4fee\u6539");
        button1.setBackground(Color.white);
        button1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 12));
        button1.setIcon(new ImageIcon(getClass().getResource("/images/a.gif")));
        button1.addActionListener(e -> button1ActionPerformed(e));

        //---- button2 ----
        button2.setText("\u91cd\u7f6e");
        button2.setBackground(Color.white);
        button2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 12));
        button2.addActionListener(e -> button2ActionPerformed(e));

        //---- comboBox1 ----
        comboBox1.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u826f\u597d",
            "\u4f18\u79c0",
            "\u8f83\u5dee",
            "\u975e\u5e38\u5dee"
        }));
        comboBox1.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 14));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(54, 54, 54)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label4)
                        .addComponent(label1)
                        .addComponent(label3))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(textField1)
                        .addComponent(textField2)
                        .addComponent(comboBox1, GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                    .addGap(49, 49, 49))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap(42, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addGap(18, 18, 18)
                    .addComponent(button2, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1))
                    .addGap(16, 16, 16)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label4))
                    .addGap(17, 17, 17)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
                    .addGap(20, 20, 20))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JTextField textField1;
    private JLabel label1;
    private JTextField textField2;
    private JLabel label3;
    private JLabel label4;
    private JButton button1;
    private JButton button2;
    private JComboBox<String> comboBox1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
